#!/bin/bash
#
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# DEPRECATED
#

# This script shows the path of the active toolchain components within
# the ndk. This was necessary for GCC and binutils, where each ABI had
# its own tools, but is not needed for LLVM-based tools which should be
# used in preference.

usage() {
  echo "USAGE: ndk-which [--abi ABI] TOOL"
  echo "ABI is 'armeabi-v7a', 'arm64-v8a', 'x86', or 'x86_64'"
  echo "TOOL is 'gdb', 'objdump', 'readelf', etc."
  echo
  echo "Note that LLVM replacements for binutils tools work for all ABIs."
  exit 1
}

ABI=armeabi-v7a

while (( "$#" )); do
  case "$1" in
    --abi)
      ABI=$2
      shift 2
      abis='^(armeabi-v7a|arm64-v8a|x86|x86_64)$'
      if [[ ! "$ABI" =~ $abis ]]; then usage; fi
      ;;
    *)
      break
      ;;
  esac
done

TOOL=$1
shift

if [ "$#" != 0 -o "$TOOL" == "" ]; then
  usage
fi

# This tool is installed in prebuilt/linux-x86_64/bin/.
MYNDKDIR=`dirname $0`/../../..

# create a temporary skeleton project so that we can leverage build-local.mk
TMPDIR=/tmp/ndk-which-$$
mkdir -p $TMPDIR/jni
cat >$TMPDIR/jni/Android.mk << "END_OF_FILE"
include $(CLEAR_VARS)
END_OF_FILE

get_build_var_for_abi() {
  if [ -z "$GNUMAKE" ] ; then
    GNUMAKE=make
  fi
  NDK_PROJECT_PATH=$TMPDIR $GNUMAKE --no-print-dir -f $MYNDKDIR/build/core/build-local.mk DUMP_$1 APP_ABI=$2
}

TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $ABI`
rm -Rf $TMPDIR

# fully qualified file name
FQFN=${TOOLCHAIN_PREFIX}$TOOL

# use the host system's 'which' to decide/report if the file exists or not, and is executable
which "$FQFN"
